package com.yingxue.lesson.controller;

import com.yingxue.lesson.contants.Constant;
import com.yingxue.lesson.entity.SysFile;
import com.yingxue.lesson.service.FileService;
import com.yingxue.lesson.utils.DataResult;
import com.yingxue.lesson.utils.JwtTokenUtil;
import com.yingxue.lesson.vo.req.fileVo.FilePageReqVO;
import com.yingxue.lesson.vo.resp.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api")
@Api(tags = "文件上传模块")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/file")
    @ApiOperation(value = "文件上传接口")
    public DataResult upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        String userId = JwtTokenUtil.getUserId(request.getHeader(Constant.ACCESS_TOKEN));
        int type = request.getIntHeader(Constant.FILE_TYPE);
        String upload = fileService.upload(file, userId, type);
        return DataResult.success(upload);
    }


    @GetMapping("/file/{fileId}")
    @ApiOperation(value = "文件下载接口")
    public void download(@PathVariable("fileId") String fileId, HttpServletResponse response){
        fileService.download(fileId,response);
    }


    @ApiOperation("分页获取我的文件接口")
    @PostMapping("/files")
    public DataResult<PageVO<SysFile>> pageInfo(@RequestBody FilePageReqVO vo,HttpServletRequest request){
        String userId = JwtTokenUtil.getUserId(request.getHeader(Constant.ACCESS_TOKEN));
        PageVO<SysFile> sysFilePageVO = fileService.pageInfo(vo, userId);
        DataResult result = DataResult.success();
        result.setData(sysFilePageVO);
        return result;
    }


}
